# 确定url地址
# 发送网络请求
# 数据解析
# 数据保存

import  requests
import csv
import pprint

file= open('data.csv', mode="a", encoding="utf-8", newline="")
csv_write=csv.DictWriter(file,fieldnames=['股票代码','股票名称','当前额','涨跌额'])
csv_write.writeheader()
# 1.定url地址
url ='https://xueqiu.com/service/v5/stock/screener/quote/list?page=1&size=30&order=desc&order_by=amount&exchange=CN&market=CN&type=sha&_=1625171238658'
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
# 2.发送网络请求
response=requests.get(url=url,headers=headers)
json_data=response.json()
# pprint.pprint(json_data)

# 3.数据解析
data_list=json_data['data']['list']


for data in data_list:
 # print(data)
 data_dict={}
 data1 = data['symbol']
 data2 = data['name']
 data3 = data['current']
 data4 = data['symbol']
 # data5 = data['symbol']
 # data6 = data['symbol']
 # data7 = data['symbol']
 # data8 = data['symbol']
 # data9 = data['symbol']
 # data10 = data['symbol']
 print(data1,data2,data3,data4)
 data_dict={'股票代码':data1,'股票名称':data2,'当前额':data3,'涨跌额':data4}
 csv_write.writerow(data_dict)